import { createDiscreteApi } from 'naive-ui'
import {ref} from 'vue'



const { dialog } = createDiscreteApi(["dialog"])


export  const showFriendCircle = ref(false)
export  const treeHoleList = ref([])
export  const weiYanDialogVisible = ref(false)
export  const isPublic = ref(true)
export  const weiYanAvatar = ref('')
export  const weiYanUsername = ref('')
export  const pagination = ref({
    current: 1,
    size: 10,
    total: 0,
    userId: null
  })

export  function launch() {
    weiYanDialogVisible.value = true
  }

export  function openFriendCircle(userId, avatar, username) {
    pagination.value.userId = userId
    weiYanAvatar.value = avatar
    weiYanUsername.value = username
    getWeiYan()
  }

export  function deleteTreeHole(id) {
    dialog.error({
      title: '警告',
      content: '确定删除?',
      positiveText: '确定',
      onPositiveClick: () => {
        // $http.get($constant.baseURL + '/weiYan/deleteWeiYan', { id: id })
        //   .then((res) => {
        //     ElMessage({
        //       message: '删除成功！',
        //       type: 'success'
        //     })
        //     pagination.value.current = 1
        //     pagination.value.size = 10
        //     treeHoleList.value = []
        //     getWeiYan()
        //   })
        //   .catch((error) => {
        //     ElMessage({
        //       message: error.message,
        //       type: 'error'
        //     })
        //   })
      }
    })
  }

export  function getWeiYan() {
    // $http.post($constant.baseURL + '/weiYan/listWeiYan', pagination.value)
    //   .then((res) => {
    //     if (!$common.isEmpty(res.data)) {
    //       res.data.records.forEach(c => {
    //         c.content = c.content.replace(/\n{2,}/g, '<div style="height: 12px"></div>')
    //         c.content = c.content.replace(/\n/g, '<br/>')
    //         c.content = $common.faceReg(c.content)
    //         c.content = $common.pictureReg(c.content)
    //       })
    //       treeHoleList.value = treeHoleList.value.concat(res.data.records)
    //       pagination.value.total = res.data.total
    //       showFriendCircle.value = true
    //     }
    //   })
    //   .catch((error) => {
    //     ElMessage({
    //       message: error.message,
    //       type: 'error'
    //     })
    //   })
  }

export  function submitWeiYan(content) {
    const weiYan = {
      content: content,
      isPublic: isPublic.value
    }

    // $http.post($constant.baseURL + '/weiYan/saveWeiYan', weiYan)
    //   .then((res) => {
    //     pagination.value.current = 1
    //     pagination.value.size = 10
    //     treeHoleList.value = []
    //     weiYanDialogVisible.value = false
    //     getWeiYan()
    //   })
    //   .catch((error) => {
    //     ElMessage({
    //       message: error.message,
    //       type: 'error'
    //     })
    //   })
  }

export  function cleanFriendCircle() {
    pagination.value = {
      current: 1,
      size: 10,
      total: 0,
      userId: null
    }
    weiYanAvatar.value = ''
    weiYanUsername.value = ''
    treeHoleList.value = []
    showFriendCircle.value = false
  }

export  function pageWeiYan() {
    pagination.value.current = pagination.value.current + 1
    getWeiYan()
  }

export  function addFriend() {
    dialog.success({
      title: '好友申请',
      content: '确认提交好友申请，添加 ' + weiYanUsername.value + ' 为好友？',
      positiveText: '确定',
      onPositiveClick: () => {
        $http.get($constant.baseURL + '/imChatUserFriend/addFriend', { friendId: pagination.value.userId })
          .then((res) => {
            ElMessage({
              message: '提交成功！',
              type: 'success'
            })
          })
          .catch((error) => {
            ElMessage({
              message: error.message,
              type: 'error'
            })
          })
      }
    })
  }


